package com.whu.assignment8.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whu.assignment8.common.Result;
import com.whu.assignment8.entity.Supplier;
import com.whu.assignment8.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/supplier")
public class supplierController {

    @Autowired
    private ISupplierService supplierService;

    /**
     * 新增或更新商品
     */
    @PostMapping
    public Result save(@RequestBody Supplier supplier){
        return Result.success(supplierService.saveOrUpdate(supplier));
    }

    /**
     * 查询所有商品
     * @return
     */
    @GetMapping
    public Result findAll(){
        System.out.println(supplierService.list());
        return Result.success(supplierService.list());
    }

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id){
        return Result.success(supplierService.getById(id));
    }

    /**
     * 通过名字查询商品
     * @param name
     * @return
     */
    @GetMapping("/name/{name}")
    public Result findOne(@PathVariable String name){
        QueryWrapper<Supplier> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return Result.success(supplierService.getOne(queryWrapper));
    }

    /**
     * 通过类型查询商品
     * @param tel
     * @return
     */
    @GetMapping("/tel/{tel}")
    public Result findByType(@PathVariable String tel){
        QueryWrapper<Supplier> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",tel);
        return Result.success(supplierService.getOne(queryWrapper));
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        return Result.success(supplierService.removeById(id));
    }
}
